import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios'

const api: AxiosInstance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 10000
})
api.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('jwtToken')
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  (error) => Promise.reject(error)
)

export function request<T>(config: AxiosRequestConfig): Promise<AxiosResponse<T>> {
  return api(config)
}

export default api
